from django.shortcuts import render

from rest_framework.views import APIView
from mutils import YunPian
from rest_framework.response import Response
from .sers import *
from .others import *
from rest_framework.viewsets import ModelViewSet
from rest_framework.authentication import TokenAuthentication
from django_filters.rest_framework import DjangoFilterBackend



class CodeAPIView(APIView):
    throttle_classes = [CodeThrottle]

    def post(self, request):
        ser = CodeSerializer(data=request.data)
        ser.is_valid(raise_exception=True)
        phone = ser.validated_data.get("phone")
        res = YunPian.send_sms(phone)

        if res["code"] != 0:
            return Response({"msg": res["msg"]})

        Code.objects.create(phone=phone, code=YunPian.get_code())

        return Response({"msg": "发送成功", "code": YunPian.get_code()})


class UserViewSet(ModelViewSet):
    queryset = User.objects.all()

    serializer_class = UserSerialzier


class BookViewSet(ModelViewSet):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
    authentication_classes = [TokenAuthentication]
    permission_classes = [SVIPPermission]


class HeroViewSet(ModelViewSet):
    queryset = Hero.objects.all()
    serializer_class = HeroSerializer
    pagination_class = HeroPage
    filter_backends = [DjangoFilterBackend]
    filter_class =HeroFilterSet
    throttle_classes = [HeroThrottle]
